package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LanekeyMapper;
import com.ruoyi.system.domain.LaneKey;
import com.ruoyi.system.service.ILanekeyService;
import com.ruoyi.common.core.text.Convert;

/**
 * 按键记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-17
 */
@Service
public class LanekeyServiceImpl implements ILanekeyService 
{
    @Autowired
    private LanekeyMapper lanekeyMapper;

    /**
     * 查询按键记录
     * 
     * @param tradeId 按键记录主键
     * @return 按键记录
     */
    @Override
    public LaneKey selectLanekeyByTradeId(String tradeId)
    {
        return lanekeyMapper.selectLanekeyByTradeId(tradeId);
    }

    /**
     * 查询按键记录列表
     * 
     * @param lanekey 按键记录
     * @return 按键记录
     */
    @Override
    public List<LaneKey> selectLanekeyList(LaneKey lanekey)
    {
        return lanekeyMapper.selectLanekeyList(lanekey);
    }
//
//    @Override
//    public Map<String, Boolean> existsByTradeIds(List<String> tradeIds) {
//        if (tradeIds == null || tradeIds.isEmpty()) {
//            return Collections.emptyMap();
//        }
//
//        // 1. 批量查询存在的trade_id（自动去重）
//        Set<String> existingIds = lanekeyMapper.selectExistingTradeIds(tradeIds);
//
//        // 2. 转换为Map：存在的ID标记为true，不存在的默认false
//        return tradeIds.stream()
//                .collect(Collectors.toMap(
//                        id -> id,
//                        existingIds::contains  // 等价于 id -> existingIds.contains(id)
//                ));
//    }

//    @Override
//    public Set<String> getExistingTradeIds(List<String> tradeIds, String tableName) {
//        if (tradeIds == null || tradeIds.isEmpty()) {
//            return Collections.emptySet();
//        }
//
//        Set<String> result = new HashSet<>();
//        int batchSize = 1000;
//
//        // 分批处理
//        for (int i = 0; i < tradeIds.size(); i += batchSize) {
//            int toIndex = Math.min(i + batchSize, tradeIds.size());
//            List<String> batchIds = tradeIds.subList(i, toIndex);
//
//            if (!batchIds.isEmpty()) {
//                List<String> existingIds = lanekeyMapper.selectExistingTradeIds(batchIds, tableName);
//                result.addAll(existingIds);
//            }
//        }
//
//        return result;
//    }

    @Override
    public Set<String> getExistingTradeIds(String tableName) {
       return lanekeyMapper.selectExistingTradeIds(tableName);
    }

    /**
     * 新增按键记录
     * 
     * @param lanekey 按键记录
     * @return 结果
     */
    @Override
    public int insertLanekey(LaneKey lanekey)
    {
        lanekey.setCreateTime(DateUtils.getNowDate());
        return lanekeyMapper.insertLanekey(lanekey);
    }

    /**
     * 修改按键记录
     * 
     * @param lanekey 按键记录
     * @return 结果
     */
    @Override
    public int updateLanekey(LaneKey lanekey)
    {
        return lanekeyMapper.updateLanekey(lanekey);
    }

    /**
     * 批量删除按键记录
     * 
     * @param tradeIds 需要删除的按键记录主键
     * @return 结果
     */
    @Override
    public int deleteLanekeyByTradeIds(String tradeIds)
    {
        return lanekeyMapper.deleteLanekeyByTradeIds(Convert.toStrArray(tradeIds));
    }

    /**
     * 删除按键记录信息
     * 
     * @param tradeId 按键记录主键
     * @return 结果
     */
    @Override
    public int deleteLanekeyByTradeId(String tradeId)
    {
        return lanekeyMapper.deleteLanekeyByTradeId(tradeId);
    }
}
